﻿namespace GameKits.Unity.Rts
{
    using UnityEngine;

    public class UnitBehaviour : MonoBehaviour
    {
        private UnitBase unit;

        private bool initialized;

        public T AttachUnit<T>() where T : UnitBase, new()
        {
            this.initialized = false;

            var newUnit = new T();
            this.unit = newUnit;
            return newUnit;
        }

        public void Update()
        {
            if (this.unit == null)
            {
                return;
            }

            if (!this.initialized)
            {
                this.unit.Initialize(this.transform, this.gameObject);
                this.initialized = true;
            }

            this.unit.Update(Time.deltaTime);
        }
    }
}
